package rentadomus

import org.springframework.dao.DataIntegrityViolationException

class ApartamentController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [apartamentInstanceList: Apartament.list(params), apartamentInstanceTotal: Apartament.count()]
    }

    def create() {
        [apartamentInstance: new Apartament(params)]
    }
    
    def search() {
        
    }
    def save() {
        def apartmentInstance = new Apartament(params)
        def dwellingInstance = new Dwelling(params)
        def placeInstance = new Place(params)
        placeInstance.idPerson = session.person
        apartmentInstance.idDwelling = dwellingInstance
        dwellingInstance.idPlace= placeInstance
        if (placeInstance.save(flush: true)) {
            if(dwellingInstance.save(flush: true)){
                if (!apartmentInstance.save(flush: true)) {
                    render(view: "create", model: [apartmentInstance: apartmentInstance])
                    return
                }
            }
        }
        redirect(controller:"uploadImage", action: "main", id: placeInstance.id)
    }

    def show(Long id) {
        def apartamentInstance = Apartament.get(id)
        if (!apartamentInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'apartament.label', default: 'Apartament'), id])
            redirect(action: "list")
            return
        }
        def dwellingInstance=apartamentInstance.idDwelling
        def placeInstance = Place.get(dwellingInstance.idPlace.id)
        def visible=session.person && placeInstance.getIdPerson().getId()==session.person.id
        [apartamentInstance: apartamentInstance,placeInstance: placeInstance,dwellingInstance:dwellingInstance,phones:Phone.findAllByIdPerson(placeInstance.getIdPerson()),visible:visible]
    }
    def edit(Long id) {
        def apartamentInstance = Apartament.get(id)
        if (!apartamentInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'apartament.label', default: 'Apartament'), id])
            redirect(action: "list")
            return
        }

        [apartamentInstance: apartamentInstance]
    }

    def update(Long id, Long version) {
        def apartamentInstance = Apartament.get(id)
        if (!apartamentInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'apartament.label', default: 'Apartament'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (apartamentInstance.version > version) {
                apartamentInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'apartament.label', default: 'Apartament')] as Object[],
                          "Another user has updated this Apartament while you were editing")
                render(view: "edit", model: [apartamentInstance: apartamentInstance])
                return
            }
        }

        apartamentInstance.properties = params

        if (!apartamentInstance.save(flush: true)) {
            render(view: "edit", model: [apartamentInstance: apartamentInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'apartament.label', default: 'Apartament'), apartamentInstance.id])
        redirect(action: "show", id: apartamentInstance.id)
    }

    def delete(Long id) {
        def warehouseInstance = Apartament.get(id)
        
        if (!warehouseInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "list")
            return
        }
        def hoesInstance = Dwelling.get(warehouseInstance.idDwelling.id)
        def placeInstance = Place.get(hoesInstance.idPlace.id)
        
        try {
            warehouseInstance.delete(flush: true)
            hoesInstance.delete(flush: true)
            placeInstance.delete(flush: true)
            
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "show", id: id)
        }
    }
    
       def filterSearch(Integer max){
        def option = params.option
        if(option=="1"){
            def filter = params.filter
            def fVal=params.value
            def tVal= params.value2
            params.max = Math.min(max ?: 10, 100)
            [apartamentInstanceList: Apartament.list(params), apartamentInstanceTotal: Apartament.count(), valueP: fVal, 
                valueP2: tVal, placeInstanceList:Place.list(params), filterB: filter]
        }
        else{
            def value = params.value
            def filter = params.filter
            println(value)
            println(filter)
            params.max = Math.min(max ?: 10, 100)
            [apartamentInstanceList: Apartament.list(params), apartamentInstanceTotal: Apartament.count(), valueP: value, 
                placeInstanceList:Place.list(params), filterB: filter]
        }
    }
}
